package org.sklse.learningassistant.service;

import org.sklse.learningassistant.bean.User;
import org.sklse.learningassistant.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author masker
 * @date 2018/5/6 上午11:24
 */
@Service
public class UserSerivce {

    @Autowired
    private UserRepository userRepository;

    public boolean exist(String userName) {
        User user = userRepository.findByusername(userName);
        return (user != null);
    }

    public boolean register(User user) {
        System.out.println("register start...");
        System.out.println("username: " + user.getUsername());
        System.out.println("passowrd: " + user.getPassword());
        if (exist(user.getUsername())) {
            System.out.println("register failed!");
            return false;
        } else {
            userRepository.save(user);
            System.out.println("register success!");
            return true;
        }
    }

    public boolean login(User login) {
        User user = userRepository.findByusername(login.getUsername());
        if (user == null) {
            return false;
        }
        if (!user.getPassword().equals(login.getPassword())) {
            return false;
        }
        return true;
    }
}
